package com.chuantou.yuba.ui.widget;

import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.OtherUserTO;
import com.chuantou.yuba.api.to.UserTO;
import com.chuantou.yuba.ui.OthersProfileActivity;
import com.chuantou.yuba.util.AccountManager;
import com.chuantou.yuba.util.ActivityHelper;
import com.chuantou.yuba.util.UiUtil;

public class OtherProfilePopup extends PopupWindow implements View.OnClickListener {

	private View mContentView;
	private TextView mBlackListView;
	private TextView mFollowView;
	private TextView mReportView;
	private OthersProfileActivity mActivity;
	private OtherUserTO mUser;

	public OtherProfilePopup(OthersProfileActivity activity, OtherUserTO user) {
		super(activity);
		mActivity = activity;
		this.mUser = user;
		mContentView = View.inflate(activity, R.layout.other_profile_popup, null);
		AccountManager accountManager = AccountManager.getInstance(activity.getApplicationContext());
		UserTO loginUser = accountManager.getUserTO();
		mBlackListView = (TextView) mContentView.findViewById(R.id.black_list);
		mFollowView = (TextView) mContentView.findViewById(R.id.follow);
		mReportView = (TextView) mContentView.findViewById(R.id.report);
		if (loginUser == null || loginUser.getUid() == user.getUid()) {
			mBlackListView.setVisibility(View.GONE);
			mFollowView.setVisibility(View.GONE);
			mReportView.setVisibility(View.GONE);
		}
		mBlackListView.setText(user.isBlocked() ? R.string.remove_black_list : R.string.add_black_list);
		mBlackListView.setOnClickListener(this);
		mFollowView.setOnClickListener(this);
		mFollowView.setText(user.isFollowed() ? R.string.remove_follow : R.string.follow);
		mContentView.setOnClickListener(this);
		mReportView.setOnClickListener(this);
		mContentView.findViewById(R.id.cancel).setOnClickListener(this);
		setContentView(mContentView);
		setHeight(FrameLayout.LayoutParams.MATCH_PARENT);
		setWidth(FrameLayout.LayoutParams.MATCH_PARENT);
		setFocusable(true);
		setAnimationStyle(R.style.popup_style);
		ColorDrawable drawable = new ColorDrawable(activity.getResources().getColor(R.color.translucence));
		setBackgroundDrawable(drawable);
	}

	public void dismissPopup() {
		Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.to_bottom);
		mContentView.clearAnimation();
		animation.setFillAfter(true);
		animation.setAnimationListener(new DismissAnimationListener() {

			@Override
			public void onAnimationEnd(Animation animation) {
				super.onAnimationEnd(animation);
				new Handler().post(new Runnable() {

					@Override
					public void run() {
						OtherProfilePopup.super.dismiss();
					}
				});
			}
		});
		mContentView.setAnimation(animation);
	}

	@Override
	public void dismiss() {
		dismissPopup();
	}

	@Override
	public void showAsDropDown(View anchor, int xoff, int yoff) {
		super.showAsDropDown(anchor, xoff, yoff);
		startShowAnimation();
	}

	@Override
	public void showAtLocation(View parent, int gravity, int x, int y) {
		super.showAtLocation(parent, gravity, x, y);
		startShowAnimation();
	}

	private void startShowAnimation() {
		mContentView.clearAnimation();
		Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.from_bottom);
		animation.setFillAfter(true);
		mContentView.setAnimation(animation);
	}

	class DismissAnimationListener implements Animation.AnimationListener {

		private int position = -1;

		public DismissAnimationListener() {
		}

		public DismissAnimationListener(int position) {
			this.position = position;
		}

		public int getPosition() {
			return position;
		}

		@Override
		public void onAnimationStart(Animation animation) {

		}

		@Override
		public void onAnimationEnd(Animation animation) {

		}

		@Override
		public void onAnimationRepeat(Animation animation) {

		}

	}

	@Override
	public void onClick(View v) {
		if (UiUtil.isFastDoubleClick()) {
			return;
		}
		dismiss();

		if (v == mBlackListView) {
			mActivity.onBlackListClick();
		} else if (v == mFollowView) {
			mActivity.onFollowClick();
		} else if (v == mReportView) {
			ActivityHelper.toReportActivity(mActivity, mUser.getUid());
		} else if (v == mContentView || v.getId() == R.id.cancel) {
			dismiss();
		}
	}
}